package com.data.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.FeedbackInfoVo;
import com.data.core.dao.FeedBackInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.data.core.entry.FeedbackInfo;
import com.data.core.service.FeedBackInfoService;
import javax.annotation.Resource;
import java.util.Objects;

/**
 * @version: V1.0
 * @author: szw
 * @className: FeedBackInfoServiceImpl
 * @packageName: FeedBackInfoServiceImpl
 * @description: 意见接口实现
 * @data: 2021-1-22
 **/
@Service
public class FeedBackInfoServiceImpl extends ServiceImpl<FeedBackInfoMapper, FeedbackInfo> implements FeedBackInfoService {

    @Resource
    private FeedBackInfoMapper feedBackInfoMapper;

    /**
     * 保存意见
     * @param feedbackInfoVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-保存意见",operationType=BaseEnum.INSERT)
    public int save(FeedbackInfoVo feedbackInfoVo) throws Exception {
        if (Objects.isNull(feedbackInfoVo)) {
            throw new BaseException(BaseEnum.FEED_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(feedbackInfoVo, true);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.SAVE_FEED_DATA_IS_ERROR);
        }
    }

    /**
     * 更新意见
     * @param feedbackInfoVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-更新意见",operationType=BaseEnum.UPDATE)
    public int update(FeedbackInfoVo feedbackInfoVo) throws Exception {
        if (Objects.isNull(feedbackInfoVo)) {
            throw new BaseException(BaseEnum.FEED_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(feedbackInfoVo, false);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.UPDATE_FEED_DATA_IS_ERROR);
        }
    }

    /**
     * 查询意见
     * @param id
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-查询意见",operationType=BaseEnum.SELECT)
    public FeedbackInfo findFeedbackInfoById(Long id) throws Exception {
        if (null == id) {
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return feedBackInfoMapper.selectById(id);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_FEED_DATA_IS_ERROR);
        }
    }

    /**
     * 删除意见
     * @param id
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-删除意见",operationType=BaseEnum.DELETE)
    public int delete(Long id) throws Exception {
        if (null == id) {
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return feedBackInfoMapper.deleteById(id);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.DELETE_FEED_DATA_IS_ERROR);
        }
    }

    /**
     * 分页查询意见列表
     * @param page
     * @param feedbackInfoVo
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "核心服务-分页查询意见列表",operationType=BaseEnum.SELECT)
    public IPage<FeedbackInfo> feedbackInfoPage(Page<FeedbackInfo> page, FeedbackInfoVo feedbackInfoVo) throws Exception {
        if (Objects.isNull(feedbackInfoVo)) {
            throw new BaseException(BaseEnum.FEED_DATA_IS_NULL);
        }
        try {
            QueryWrapper<FeedbackInfo> queryWrapper = new QueryWrapper<>();
            return feedBackInfoMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_FEED_DATA_IS_ERROR);
        }
    }

    /**
     * 更新、保存意见
     *
     * @param feedbackInfoVo
     * @param flag           true 保存 false 更新
     * @return
     */
    private int saveToUpdate(FeedbackInfoVo feedbackInfoVo, Boolean flag) {
        FeedbackInfo notice = new FeedbackInfo();
        BeanUtils.copyProperties(feedbackInfoVo, notice);
        if (flag) {
            return feedBackInfoMapper.insert(notice);
        }
        return feedBackInfoMapper.updateById(notice);
    }
}
